package cz.fel.via.timemanagement;

import cz.cvut.fel.via.kosapirest.Fasade;
import cz.cvut.fel.via.kosapirest.domain.*;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class OneCourseActivity extends Activity {

	private final static int ONLY_ME_NOTES = 15;
	private final static int ONLY_ME_EXAMS = 25;
	LinearLayout placeForNotes;
	String username;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.one_course);

		// course = plny kod predmetu
		String courseCode = (String) TimeManagement.getKey("course");
		if (courseCode == null) {
			// TODO chyba :-)
		}

		username = (String) TimeManagement.getKey("username");
		if (username == null) {
			username = TimeManagement.getUser();
		}

		Fasade fasade = TimeManagement.getFasade();
		Course course = fasade.find(courseCode, Course.class);

		TextView name = (TextView) findViewById(R.id.courseName);
		name.setText(course.getName());
		name.setGravity(Gravity.CENTER_HORIZONTAL);

		TextView description = (TextView) findViewById(R.id.description);
		description.setText(course.getDescription());

		TextView courseCodeView = (TextView) findViewById(R.id.courseCode);
		courseCodeView.setText(courseCode);

		TextView web = (TextView) findViewById(R.id.web);
		if (course.getHomepage()!=null&&!course.getHomepage().isEmpty()) {
			SpannableString content = new SpannableString(course.getHomepage());
			content.setSpan(new UnderlineSpan(), 0, course.getHomepage()
					.length(), 0);
			web.setText(content);
			web.setTextColor(Color.BLUE);

			final String homepage = course.getHomepage();

			web.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setAction(Intent.ACTION_VIEW);
					intent.addCategory(Intent.CATEGORY_BROWSABLE);
					intent.setData(Uri.parse(homepage));
					startActivity(intent);
				}
			});
		}else{
			web.setText("No data for course home web page available...");
			
		}

		TextView credits = (TextView) findViewById(R.id.credits);
		credits.setText("" + course.getCredits());

		TextView range = (TextView) findViewById(R.id.range);
		range.setText(course.getRange());

		TextView note = (TextView) findViewById(R.id.note);
		note.setText(course.getNote());
	}

	public void student(View v) {
		TimeManagement.putKey("username", username);

		Intent intent = new Intent(this, StudentActivity.class);
		startActivity(intent);
	}

	public void schedule(View v) {
		TimeManagement.putKey("username", username);

		Intent intent = new Intent(this, ScheduleActivity.class);
		startActivity(intent);
	}

	public void notes(View v) {
		if (username.equals(TimeManagement.getUser())) {
			Intent intent = new Intent(this, NoteActivity.class);
			startActivity(intent);
		} else {
			showDialog(ONLY_ME_NOTES);
		}
	}

	public void exams(View v) {
		if (username.equals(TimeManagement.getUser())) {
			Intent intent = new Intent(this, ExamActivity.class);
			startActivity(intent);
		} else {
			showDialog(ONLY_ME_NOTES);
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case ONLY_ME_NOTES:
			AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
			alertbox.setMessage(R.string.onlyMeNotes);
			alertbox.setNeutralButton("Ok", null);
			return alertbox.show();
		case ONLY_ME_EXAMS:
			AlertDialog.Builder alertbox1 = new AlertDialog.Builder(this);
			alertbox1.setMessage(R.string.onlyMeExams);
			alertbox1.setNeutralButton("Ok", null);
			return alertbox1.show();
		}
		return null;
	}
}
